package org.microservice.system.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.microservice.system.dao.SysUserDataScopeDao;
import org.microservice.system.entity.SysUserDataScopeEntity;
import org.microservice.system.service.SysUserDataScopeService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/7/16
 * Time: 17:22
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class SysUserDataScopeServiceImpl extends ServiceImpl<SysUserDataScopeDao,SysUserDataScopeEntity> implements SysUserDataScopeService {
    @Override
    public void saveOrUpdate(Integer userId, Map<String,List<Integer>> dataScopes) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",userId);
        deleteByMap(map);
        ArrayList<SysUserDataScopeEntity> entities = new ArrayList<>();

        Set<String> keySet = dataScopes.keySet();
        for (String s :
                keySet) {
            List<Integer> integers = dataScopes.get(s);
            for (int i : integers){
                SysUserDataScopeEntity sysUserDataScopeEntity = new SysUserDataScopeEntity(userId, i,s);
                entities.add(sysUserDataScopeEntity);
            }
        }
        insertBatch(entities);
    }
}
